//
//  YLCSynchronizeView.m
//  LinYa
//
//  Created by Fancy on 2018/7/10.
//  Copyright © 2018年 初程程. All rights reserved.
//

#import "YLCSynchronizeView.h"
#import <ReactiveCocoa/RACReturnSignal.h>

@interface YLCSynchronizeView()
{
    UILabel *titleLabel;
    UILabel *numLabel;
    UILabel *promtLabel;
    UIView *buyBackView;
}
@end

@implementation YLCSynchronizeView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        [self setupSubViews];
    }
    return self;
}
- (void)setupSubViews{
    UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"hud_background"]];
    
    backgroundView.userInteractionEnabled = YES;
    
    [self addSubview:backgroundView];
    
    [backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    UIImageView *headIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"buy_head"]];
    
    [backgroundView addSubview:headIconView];
    
    [headIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(backgroundView.mas_top).offset(-10);
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.width.equalTo(100);
        make.height.equalTo(98);
    }];
        
    titleLabel = [[UILabel alloc] init];
    
    titleLabel.textColor = YLC_THEME_COLOR;
    
    titleLabel.text = @"输入模板码";
    
    [backgroundView addSubview:titleLabel];
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.top.equalTo(headIconView.mas_bottom).offset(40);
    }];
    
    numLabel = [[UILabel alloc] init];
    
    numLabel.textColor = [UIColor lightGrayColor];
    
    numLabel.hidden = YES;
    
    [backgroundView addSubview:numLabel];
    
    [numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.top.equalTo(titleLabel.mas_bottom).offset(40);
    }];
    
    promtLabel = [[UILabel alloc] init];
    
    promtLabel.textColor = [UIColor lightGrayColor];
    
    promtLabel.text = @"(24小时内有效)";
    
    promtLabel.hidden = YES;
    
    [backgroundView addSubview:promtLabel];
    
    [promtLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.top.equalTo(numLabel.mas_bottom).offset(40);
    }];
    
    _numField = [[UITextField alloc] init];
    
    _numField.placeholder = @" 请输入模板码";
    
    _numField.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    
    _numField.keyboardType = UIKeyboardTypeNumberPad;
    
    _numField.textAlignment = NSTextAlignmentCenter;
    
    [self addSubview:_numField];
    
    [_numField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    [_numField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.top.equalTo(titleLabel.mas_bottom).offset(40);
        make.width.equalTo(130);
        make.height.equalTo(40);
    }];
    
    buyBackView = [[UIView alloc] init];
    
    buyBackView.layer.shadowOffset = CGSizeMake(0, 3);
    
    buyBackView.layer.shadowOpacity = 0.2;
    
    buyBackView.layer.shadowColor = [UIColor blackColor].CGColor;
    
    [backgroundView addSubview:buyBackView];
    
    UIButton *buyBtn = [YLCFactory creatCornerAndBorderWithCornerRadius:20 title:@"确认输入" backgroundColor:[UIColor whiteColor] textColor:YLC_THEME_COLOR borderColor:YLC_THEME_COLOR borderWidth:1];
    
    [buyBtn addTarget:self action:@selector(buttonTouch) forControlEvents:UIControlEventTouchUpInside];
    
    [buyBackView addSubview:buyBtn];
    
    [buyBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(backgroundView.mas_centerX);
        make.top.equalTo(_numField.mas_bottom).offset(40);
        make.width.equalTo(150);
        make.height.equalTo(40);
    }];
    
    [buyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(UIEdgeInsetsZero);
    }];
}
- (void)setType:(NSInteger)type{
    _type = type;
    if (type == 2) {
        titleLabel.text = @"生成您的模板码";
        _numField.hidden = YES;
        buyBackView.hidden = YES;
        numLabel.hidden = NO;
        promtLabel.hidden = NO;
    }
}
- (void)setCode:(NSString *)code{
    numLabel.text = code;
}
- (void)buttonTouch{
    if (JK_IS_STR_NIL(_numField.text)) {
        [SVProgressHUD showErrorWithStatus:@"请输入模板码"];
        return;
    }
    [YLCHUDShow showSelectAlertWithTarget:self Message:@"同步他人模板会覆盖现有模板，是否确认同步?" leftTitle:@"确认" rightTitle:@"取消" selector:^(NSInteger index) {
        if (index==0) {
            [self endEditing:YES];
            YLCAccountModel *userModel = [[YLCAccountManager shareManager] currentAccount];
            NSDictionary *parms = @{@"userId":userModel.user_id,
                                    @"token":userModel.token,
                                    @"code":_numField.text};
            [YLCNetWorking loadNetServiceWithParam:parms method:@"POST" urlPath:@"app/sygt/syncUser" delegate:self response:^(id responseObject, NSError *error) {
                if (error) {
                    return ;
                }
                [SVProgressHUD showSuccessWithStatus:@"同步成功"];
                [(UINavigationController *)[YLCFactory currentNav] popViewControllerAnimated:YES];
            }];
        }
    }];
}
-(void)textFieldDidChange:(UITextField *)textField{
    CGFloat maxLength = 6;
    NSString *toBeString = textField.text;
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    if (!position || !selectedRange)
    {
        if (toBeString.length > maxLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
            if (rangeIndex.length == 1){
                textField.text = [toBeString substringToIndex:maxLength];
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}
@end
